package A1_链表;

public class LC_19删除链表倒数第N个节点 {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyhead=new ListNode();
        dummyhead.next=head;
        ListNode fast=dummyhead;
        ListNode slow=dummyhead;
        while(n>0 && fast.next!=null){
            fast=fast.next;
            n--;//注意不要忘记n--
        }
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return dummyhead.next;
    }
}
